/*
Copyright [2020] [https://www.xiaonuo.vip]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Snowy采用APACHE LICENSE 2.0开源协议，您在使用过程中，需要注意以下几点：

1.请不要删除和修改根目录下的LICENSE文件。
2.请不要删除和修改Snowy源码头部的版权声明。
3.请保留源码和相关描述文件的项目出处，作者声明等。
4.分发源码时候，请注明软件出处 https://gitee.com/xiaonuobase/snowy-layui
5.在修改包名，模块名称，项目代码等时，请注明软件出处 https://gitee.com/xiaonuobase/snowy-layui
6.若您的项目无法满足以上几点，可申请商业授权，获取Snowy商业授权许可，请在官网购买授权，地址为 https://www.xiaonuo.vip
 */
package vip.xiaonuo.border.controller;

import vip.xiaonuo.core.annotion.BusinessLog;
import vip.xiaonuo.core.annotion.Permission;
import vip.xiaonuo.core.context.login.LoginContextHolder;
import vip.xiaonuo.core.enums.LogAnnotionOpTypeEnum;
import vip.xiaonuo.core.pojo.base.param.BaseParam;
import vip.xiaonuo.core.pojo.login.SysLoginUser;
import vip.xiaonuo.core.pojo.page.PageResult;
import vip.xiaonuo.core.pojo.response.ResponseData;
import vip.xiaonuo.core.pojo.response.SuccessResponseData;
import vip.xiaonuo.border.entity.Border;
import vip.xiaonuo.border.param.BorderParam;
import vip.xiaonuo.border.service.BorderService;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import vip.xiaonuo.order.service.OrderService;
import vip.xiaonuo.sys.modular.user.param.SysUserParam;
import vip.xiaonuo.sys.modular.user.result.SysUserResult;
import vip.xiaonuo.sys.modular.user.service.SysUserService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 销售订单控制器
 *
 * @author 陈浩
 * @date 2022-03-18 10:51:05
 */
@Controller
public class BorderController {

    private String PATH_PREFIX = "border/";

    @Resource
    private BorderService borderService;

    @Resource
    private OrderService orderService;

    @Resource
    private SysUserService sysUserService;

    /**
     * 销售订单页面
     *
     * @author 陈浩
     * @date 2022-03-18 10:51:05
     */
    @Permission
    @GetMapping("/border/index")
    public String index() {
        return PATH_PREFIX + "index.html";
    }

    /**
     * 销售订单表单页面
     *
     * @author 陈浩
     * @date 2022-03-18 10:51:05
     */
    @GetMapping("/border/form")
    public String form() {
        return PATH_PREFIX + "form.html";
    }

    /**
     * 查询销售订单
     *
     * @author 陈浩
     * @date 2022-03-18 10:51:05
     */
    @Permission
    @ResponseBody
    @GetMapping("/border/page")
    @BusinessLog(title = "销售订单_查询", opType = LogAnnotionOpTypeEnum.QUERY)
    public PageResult<Border> page(BorderParam borderParam) {
        return borderService.page(borderParam);
    }

    /**
     * 添加销售订单
     *
     * @author 陈浩
     * @date 2022-03-18 10:51:05
     */
    @Permission
    @ResponseBody
    @PostMapping("/border/add")
    @BusinessLog(title = "销售订单_增加", opType = LogAnnotionOpTypeEnum.ADD)
    public ResponseData add(@RequestBody @Validated(BorderParam.add.class) BorderParam borderParam) {
        borderService.add(borderParam);
        return new SuccessResponseData();
    }
    public SysLoginUser getLoginUser() {
        return LoginContextHolder.me().getSysLoginUserUpToDate();
    }

    @ResponseBody
    @PostMapping("/border/buy")
    public ResponseData buy(@RequestBody  BorderParam borderParam) {
        SysUserParam s = new SysUserParam();
        s.setName(borderParam.getUsername());
        BaseParam b = new BaseParam();
        List<SysUserResult> user = sysUserService.selectByUsername(s);
        System.out.println(user);

        borderParam.setCreatetime(new Date());
      //  SysLoginUser loginUser = getLoginUser();
      //  borderParam.setUsername(loginUser.getName());
        if (user.get(0).getMoney()>borderParam.getCost()){
            Double money = user.get(0).getMoney()-borderParam.getCost();
            String name = borderParam.getUsername();
            //orderService.pay(money,name);
            borderService.add(borderParam);
            return new SuccessResponseData();
        }else{
            return new SuccessResponseData(-1,"余额不足",null);
        }

    }

    /**
     * 删除销售订单
     *
     * @author 陈浩
     * @date 2022-03-18 10:51:05
     */
    @Permission
    @ResponseBody
    @PostMapping("/border/delete")
    @BusinessLog(title = "销售订单_删除", opType = LogAnnotionOpTypeEnum.DELETE)
    public ResponseData delete(@RequestBody @Validated(BorderParam.delete.class) List<BorderParam> borderParamList) {
        borderService.delete(borderParamList);
        return new SuccessResponseData();
    }

    /**
     * 编辑销售订单
     *
     * @author 陈浩
     * @date 2022-03-18 10:51:05
     */
    @Permission
    @ResponseBody
    @PostMapping("/border/edit")
    @BusinessLog(title = "销售订单_编辑", opType = LogAnnotionOpTypeEnum.EDIT)
    public ResponseData edit(@RequestBody @Validated(BorderParam.edit.class) BorderParam borderParam) {
        borderService.edit(borderParam);
        return new SuccessResponseData();
    }

    /**
     * 查看销售订单
     *
     * @author 陈浩
     * @date 2022-03-18 10:51:05
     */
    @Permission
    @ResponseBody
    @GetMapping("/border/detail")
    @BusinessLog(title = "销售订单_查看", opType = LogAnnotionOpTypeEnum.DETAIL)
    public ResponseData detail(@Validated(BorderParam.detail.class) BorderParam borderParam) {
        return new SuccessResponseData(borderService.detail(borderParam));
    }

    /**
     * 销售订单列表
     *
     * @author 陈浩
     * @date 2022-03-18 10:51:05
     */
    @Permission
    @ResponseBody
    @GetMapping("/border/list")
    @BusinessLog(title = "销售订单_列表", opType = LogAnnotionOpTypeEnum.QUERY)
    public ResponseData list(BorderParam borderParam) {
        return new SuccessResponseData(borderService.list(borderParam));
    }

}
